// 防抖函数
export function deBounce(fn, delay = 500, immediate) {
	let timer = null,
		immediateTimer = null;

	return function() {
		let args = arguments,
			context = this;

		// 第一次触发
		if (immediate && !immediateTimer) {
			fn.apply(context, args);
			//重置首次触发标识，否则下个周期执行时会受干扰
			immediateTimer = setTimeout(() => {
				immediateTimer = null;
			}, delay);
		}
		// 存在多次执行时，重置动作需放在timer中执行；
		if (immediateTimer) clearTimeout(immediateTimer);
		
		if (timer) clearTimeout(timer);

		timer = setTimeout(() => {
			fn.apply(context, args);
			timer = null;
			immediateTimer = null;
		}, delay);
	}
}

// 节流函数
export  function throttled1(fn, delay = 500,) {
    let oldtime = Date.now()
    return function (...args) {
        let newtime = Date.now()
        if (newtime - oldtime >= delay) {
            fn.apply(null, args)
            oldtime = Date.now()
        }
    }
}


